﻿using System;
using System.Collections.Generic;

namespace BlamanticUI.Components.Forms;

/// <summary>
/// Represents a &lt;select>...&lt;/select> form control.
/// </summary>
/// <typeparam name="TValue">The type of the value.</typeparam>
[HtmlTag("select")]
public class Select<TValue> : BlazorInputComponentBase<TValue>
{
    /// <summary>
    /// The option items in select control.
    /// </summary>
    [Parameter]
    #if NET6_0_OR_GREATER
    [EditorRequired]
    #endif
    public IEnumerable<SelectItem> Items { get; set; }

    protected override void OnParametersSet()
    {
        if(Items is null)
        {
            throw new ArgumentNullException(nameof(Items));
        }
    }

    protected override void AddContent(RenderTreeBuilder builder, int sequence)
    {
        foreach (var item in Items)
        {
            builder.CreateElement(0, "option", item.Text, new { value = item.Value, selected = item.Value.Equals(Value) });
        }
    }
}


public class SelectItem
{
    public string Text { get; set; }
    public object Value { get; set; }
}
